package pe.com.tma.bean;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

import org.primefaces.component.chart.series.ChartSeries;
import org.primefaces.event.DateSelectEvent;
import org.primefaces.event.SelectEvent;
import org.primefaces.model.chart.CartesianChartModel;

import pe.com.tma.model.Cotizacion;
import pe.com.tma.model.Instrumento;
import pe.com.tma.service.CotizacionService;
import pe.com.tma.service.InstrumentoService;
import pe.com.tma.util.BusquedaGrl;
import pe.com.tma.util.ParametrosGrl;

@ManagedBean(name="historicoBean")
@RequestScoped
public class HistoricoBean implements Serializable{
	
	
	public List<Cotizacion> lstCot;
	public CartesianChartModel lineChar;
	public double min;
	public BusquedaGrl busc = new BusquedaGrl();
	
	
	
	private Instrumento instumento = new Instrumento();
	private InstrumentoService  srvInst = new InstrumentoService();
	private CotizacionService srv =  new CotizacionService();
	private ParametrosGrl grl = new ParametrosGrl();
	
	public HistoricoBean(){
		//busc = new BusquedaGrl();
		lstCot = new ArrayList<Cotizacion>();
		lineChar = new CartesianChartModel();
		busc =  new BusquedaGrl();
		busc.setFecHasta(grl.getFechaActual());
		busc.setFecDesde(grl.getFechaActual());
	}
	public void buscar(ActionEvent actionEvent){
		
		lstCot = srv.obtenerHistorico(this.busc);
		cargarChar(lstCot);
		
		
	}
	private void cargarChar(List<Cotizacion> lst) {
		// TODO Auto-generated method stub
		lineChar = new CartesianChartModel();
		ChartSeries valores = new ChartSeries();
		valores.setLabel(busc.getIdStr());
		
		for(int i=0;i<lst.size();i++){
			//System.out.println(lst.get(i).getStrFecha()+ " : "+ lst.get(i).getUltcot());
			valores.set(lst.get(i).getStrFecha(), lst.get(i).getUltcot());
		}
		
		lineChar.addSeries(valores);
	}
	public List<String> completeValor(String query) {
		List<String> results = new ArrayList<String>();
		List<Instrumento> lst = new ArrayList<Instrumento>();
		
		lst = srvInst.obtenerInstrumentos(busc);
		
		for (int i = 0; i < lst.size(); i++) {
			//results.add(query + i);
			results.add(lst.get(i).getIdInstrumento());
		}
		
		return results;
	}
	
	public BusquedaGrl getBusc() {
		return busc;
	}

	public void setBusc(BusquedaGrl busc) {
		this.busc = busc;
	}
	public List<Cotizacion> getLstCot() {
		return lstCot;
	}
	public void setLstCot(List<Cotizacion> lstCot) {
		this.lstCot = lstCot;
	}
	public CartesianChartModel getLineChar() {
		return lineChar;
	}
	public void setLineChar(CartesianChartModel lineChar) {
		this.lineChar = lineChar;
	}
	public double getMin() {
		double val = 0;
		for(int i=0; i<lstCot.size();i++){
			if (i==0)val = lstCot.get(i).getUltcot();
			
			if (val > lstCot.get(i).getUltcot().doubleValue()){
				min=lstCot.get(i).getUltcot().doubleValue();
				val = min;
			}
		}
		//min = min*0.9;
		return min;
	}
	public void setMin(double min) {
		this.min = min;
	}
	public void handleSelectValor(SelectEvent event) {
		FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Selected:" + event.getObject().toString(), null);
		
		FacesContext.getCurrentInstance().addMessage(null, message);
	}
	/*
	public void select(DateSelectEvent evt){
		System.out.println(evt.getDate().toString());
	}
*/		
	
	
}
